/**
//从上到下打印出二叉树的每个节点，同一层的节点按照从左到右的顺序打印。 
//
// 
//
// 例如: 
//给定二叉树: [3,9,20,null,null,15,7], 
//
//     3
//   / \
//  9  20
//    /  \
//   15   7
// 
//
// 返回： 
//
// [3,9,20,15,7]
// 
//
// 
//
// 提示： 
//
// 
// 节点总数 <= 1000 
// 
// Related Topics 树 广度优先搜索 二叉树 👍 140 👎 0

*/

package com.xixi.dataStructure.tree.binaryTree;

import com.xixi.dataStructure.tree.TreeNode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

public class ID_Offer_32_ICongShangDaoXiaDaYinErChaShuLcof {
public static void main(String[] args) {
Solution solution = new ID_Offer_32_ICongShangDaoXiaDaYinErChaShuLcof().new Solution();
}


//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] levelOrder(TreeNode root) {

        if(root==null) return new int[0];

        List<Integer> resList = new ArrayList<>();
        Queue<TreeNode> nextLevel =new ArrayDeque<>();
        nextLevel.add(root);


        while(!nextLevel.isEmpty()){
            TreeNode nowNode = nextLevel.poll();
            resList.add(nowNode.val);
            if(nowNode.left !=null) nextLevel.add(nowNode.left);
            if(nowNode.right !=null) nextLevel.add(nowNode.right);

        }
        int[] res = new int[resList.size()];
        for(int i = 0; i < resList.size(); ++i){
            res[i] = resList.get(i);
        }

        return res;


    }
}
//leetcode submit region end(Prohibit modification and deletion)




}